package top.songxs.yygh.hosp.controller.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.songxs.yygh.common.result.Result;
import top.songxs.yygh.common.utils.HttpRequestHelper;
import top.songxs.yygh.hosp.service.DepartmentService;
import top.songxs.yygh.hosp.service.HospitalService;
import top.songxs.yygh.hosp.service.ScheduleService;
import top.songxs.yygh.model.hosp.Department;
import top.songxs.yygh.model.hosp.Hospital;
import top.songxs.yygh.model.hosp.Schedule;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/api/hosp/")
public class ApiController {
    //api/hosp/saveHospital

    @Autowired
    HospitalService hospitalService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    ScheduleService scheduleService;

    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        //前端获取参数并转换成 单值map
        Map<String, String> map = HttpRequestHelper.switchMap(request.getParameterMap());
        hospitalService.saveHospital(map);
        return Result.ok();
    }

    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, String> map = HttpRequestHelper.switchMap(request.getParameterMap());
        departmentService.save(map);
        return Result.ok();
    }
    // 上传排班  http://localhost/api/hosp/saveSchedule
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, String> map = HttpRequestHelper.switchMap(request.getParameterMap());
        scheduleService.saveSchedule(map);
        return Result.ok();
    }
    // 查询 医院 http://localhost:8201/api/hosp/hospital/show
    @PostMapping("hospital/show")
    public Result findByHosCode(HttpServletRequest request){
        Map<String, String> map = HttpRequestHelper.switchMap(request.getParameterMap());
        Hospital hospital = hospitalService.show(map);

        return Result.ok(hospital);
    }

    // 查询科室 http://localhost/api/hosp/department/list
    @PostMapping("department/list")
    public Result showDepartments(HttpServletRequest request){
        Map<String, String> map = HttpRequestHelper.switchMap(request.getParameterMap());
        Page<Department> page = departmentService.showDepartments(map);
        return Result.ok(page);
    }

    //查询排班  http://localhost/api/hosp/schedule/list

    @PostMapping("schedule/list")
    public Result showschedule(HttpServletRequest request){
        Map<String, String> map = HttpRequestHelper.switchMap(request.getParameterMap());
        Page<Schedule> page = scheduleService.showDepartments(map);
        return Result.ok(page);
    }

    // 删除科室 http://localhost:8201/api/hosp/department/remove
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, String> map = HttpRequestHelper.switchMap(request.getParameterMap());
        departmentService.remove(map);
        return Result.ok();
    }


    // 删除排班 http://localhost/api/hosp/schedule/remove
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, String> map = HttpRequestHelper.switchMap(request.getParameterMap());
        scheduleService.remove(map);
        return Result.ok();
    }


























}
